<?php

namespace App\Http\Controllers;

use App\Models\Permission;
use App\Models\PermissionCategory;
use Illuminate\Http\Request;

class PermissionsController extends Controller
{
    public function index(){
        $q = Permission::query();
        if(\request()->get('start')){
            $q->where('created_at','>=',\request()->get('start'));
        }
        if(\request()->get('end')){
            $q->where('created_at','<=',\request()->get('end'));
        }
        if(\request()->get('name')){
            $q->where('name','like',"%".\request()->get('name')."%");
        }
        if(\request()->get('display_name')){
            $q->where('display_name','like',"%".\request()->get('display_name')."%");
        }
        $permissions=$q->with('permissionCategory')->get();
        \request()->flash();
        if(\request()->wantsJson()){
            return response()->json(['code'=>0,'msg'=>'','count'=>count($permissions),'data'=>$permissions]);
        }
        return view('rbac.permissions.index',compact('permissions'));
    }

    public function create()
    {
        $permissionCategories = PermissionCategory::all();
        return view('rbac.permissions.create',compact('permissionCategories'));
    }

    public function store(Request $request)
    {
        try {
            $permission = Permission::create($request->all());
            $response = [
                'message' => 'Permission created.',
                'data' => $permission->toArray(),
            ];

            if ($request->wantsJson()) {
                return response()->json($response);
            }

            return redirect()->back()->with('message', $response['message']);
        } catch (\Exception $e) {
            if ($request->wantsJson()) {
                return response()->json([
                    'error' => true,
                    'message' => $e->getMessage()
                ]);
            }
            return redirect()->back()->withErrors($e->getMessage())->withInput();
        }
    }

    public function edit($id){
        $permission = Permission::find($id);
        $permissionCategories = PermissionCategory::all();
        return view('rbac.permissions.edit',compact('permission','permissionCategories'));
    }

    public function update(Request $request,$id){
        try {
            $permission = Permission::find($id);
            $permission->update($request->all());
            $response = [
                'message' => 'permission updated.',
                'data'    => $permission->toArray(),
            ];
            if ($request->wantsJson()) {
                return response()->json($response);
            }
            return redirect()->back()->with('message', $response['message']);
        } catch (\Exception $e) {
            if ($request->wantsJson()) {
                return response()->json([
                    'error'   => true,
                    'message' => $e->getMessage()
                ]);
            }
            return redirect()->back()->withErrors($e->getMessage())->withInput();
        }
    }

    public function destroy($id){
        $deleted = Permission::find($id)->delete();

        if (request()->wantsJson()) {
            return response()->json([
                'message' => 'permission deleted.',
                'deleted' => $deleted,
            ]);
        }

        return redirect()->back()->with('message', 'permission deleted.');
    }
}
